package com.bc66.tiger.bizsys.menucategories.validate.impl;

import org.apache.commons.lang.StringUtils;

import com.bc66.tiger.bizsys.menucategories.dto.MenuCategoriesBeanDto;
import com.bc66.tiger.bizsys.menucategories.exception.MenuCategoriesException;
import com.bc66.tiger.bizsys.menucategories.exception.MenuCategoriesMsgFactory;
import com.bc66.tiger.bizsys.menucategories.validate.IValidateForm;


public class ValidateForm implements IValidateForm <MenuCategoriesBeanDto>{
	
	public void hasError(MenuCategoriesBeanDto vbean) throws MenuCategoriesException{
		
		if(vbean.getId()!=null && !StringUtils.isNumeric(vbean.getId()))
			
			throw MenuCategoriesMsgFactory.create(1004, "用户ID");
			
		else if(null==vbean.getMenuname() || "".equals(vbean.getMenuname().trim()))
			
			throw MenuCategoriesMsgFactory.create(1000,"菜品分类名称");
		
		else if(null==vbean.getEgname() || "".equals(vbean.getEgname().trim()))
			
			throw MenuCategoriesMsgFactory.create(1000,"英文名称");
		
		else if(null==vbean.getPinyin() || "".equals(vbean.getPinyin().trim()))
			
			throw MenuCategoriesMsgFactory.create(1000,"拼音");
		
		else if(null==vbean.getSort() || "".equals(vbean.getSort().trim()))
			
			throw MenuCategoriesMsgFactory.create(1000,"排序");
		
		this.checkLength(vbean.getMenuname(), 1, 45,"菜品分类名称");
		
		this.checkLength(vbean.getEgname(), 1, 190,"英文名称");
		
		this.checkEnglish(vbean.getEgname(),"英文名称");
		
		this.checkLength(vbean.getPinyin(), 1, 190,"拼音");
		
		this.checkpinyin(vbean.getPinyin(),"拼音");
		
		if(null!=vbean.getDiscription() && !"".equals(vbean.getDiscription()))
			
			this.checkLength(vbean.getDiscription(), 0, 495,"描叙");
		
		if(!StringUtils.isNumeric(vbean.getSort()))
			
			throw MenuCategoriesMsgFactory.create(1004, "排序");
		
		this.checkLength(vbean.getSort(), 1, 5, "排序");
			
		
	}
	
	private void checkLength(final String checkStr,final int min,final int max,final String message) throws MenuCategoriesException{
		
		
			
			if(checkStr.length()<min || checkStr.length()>max){
				
				throw MenuCategoriesMsgFactory.create(1001,message);
				
			}
		
	}
	
	
	private void checkEnglish(final String checkStr,final String message)throws MenuCategoriesException{
		
		if(!this.checkRegexp(checkStr, "[ a-zA-Z'.-]+")){
			
			throw MenuCategoriesMsgFactory.create(1002,message);
		}
		
	}
	
	private void checkpinyin(final String checkStr,final String message)throws MenuCategoriesException{
		
		if(!this.checkRegexp(checkStr, "[ a-zA-Z]+")){
			
			throw MenuCategoriesMsgFactory.create(1003,message);
		}
		
	}
	
	private boolean checkRegexp(String checkStr,String regexp){
		
		return checkStr.matches(regexp);
		
	}
	
	public static void main(String[] args) {
		

		MenuCategoriesBeanDto vbean=new MenuCategoriesBeanDto();
		
		IValidateForm<MenuCategoriesBeanDto> vi=new ValidateForm();
		
		vbean.setId("1111111111111");
		
		vbean.setMenuname("fdsa'fs");
		
		vbean.setEgname("fdsa'fsf ffffffffffffffffffffffff");
		
		vbean.setPinyin("fdsa fsfffffffffffffffffffffffff");
		
		vbean.setSort("33333");
		
		try {
			
			vi.hasError(vbean);
			
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println(e.getMessage());
			//e.printStackTrace();
		}
		
		
	}
}
